package com.app.webservice;

import java.util.ArrayList;
import java.util.List;

import com.app.entidades.Album;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;

public class AlbumRest {
	
	public List<Album> getListaAlbuns() throws Exception {
		
		 String url ="http://lucianokikao.com.br/mobile/galeria.php?user=q1w2e3r4t5&pass=t5r4e3w2q1";
	     String[] resposta = new WebServiceApp().get(url);
	    
	     if (resposta[0].equals("200")) {
	         Gson gson = new Gson();
	         ArrayList<Album> listaAlbum = new ArrayList<Album>();
	         JsonParser parser = new JsonParser();
	         try{
	        	 JsonElement element = parser.parse(resposta[1]);
	        	 JsonArray array = element.getAsJsonArray();
	        	 for (int i = 0; i < array.size(); i++) {
		             listaAlbum.add(gson.fromJson(array.get(i), Album.class));
		        }
	         }catch(Exception e){
	        	 throw new Exception("Falha ao acessar Internet");
	         }
	         return listaAlbum;
	     }else {
	         throw new Exception(resposta[1]);
	     }
	 }
}
